package com.zhanggm.s3.house.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhanggm.s3.house.bo.EstateInfoBo;
import com.zhanggm.s3.house.entity.*;
import com.zhanggm.s3.house.mapper.EstateMapper;
import com.zhanggm.s3.house.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanggm
 * @since 2023-07-21
 */
@DS("2103a-house")
@Service
public class EstateServiceImpl extends ServiceImpl<EstateMapper, Estate> implements IEstateService {

    @Autowired
    private IBusinessDistrictService businessDistrictService;

    @Autowired
    private ISubwayLineService subwayLineService;

    @Autowired
    private IEstateBusinessDistrictRelationService estateBusinessDistrictRelationService;

    @Autowired
    private IEstateSubwayLineRelationService estateSubwayLineRelationService;


    /**
     * 查询管理的地铁线路和商圈
     * @param estateId
     * @return
     */
    public EstateInfoBo getEstateInfo(Integer estateId) {
        //查询地铁线路
        LambdaQueryWrapper<EstateSubwayLineRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EstateSubwayLineRelation::getEstateId, estateId);
        List<EstateSubwayLineRelation> subwayLineRelationList = estateSubwayLineRelationService.list(queryWrapper);
        List<Integer> subwayLineIdList = subwayLineRelationList.stream().map(item -> item.getSubwayLineId()).collect(Collectors.toList());
        List<SubwayLine> subwayLineList = subwayLineService.listByIds(subwayLineIdList);

        //查询商圈
        LambdaQueryWrapper<EstateBusinessDistrictRelation> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(EstateBusinessDistrictRelation::getEstateId, estateId);
        List<EstateBusinessDistrictRelation> estateBusinessDistrictRelationList = estateBusinessDistrictRelationService.list(queryWrapper2);
        List<Integer> businessDistrictIdList = estateBusinessDistrictRelationList.stream().map(item -> item.getBusinessDistrictId()).collect(Collectors.toList());
        List<BusinessDistrict> businessDistrictList = businessDistrictService.listByIds(businessDistrictIdList);

        return new EstateInfoBo(null, businessDistrictList, subwayLineList);
    }

    @Override
    public PageInfo<Estate> rankList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Estate> estateList = getBaseMapper().rankList();
        return new PageInfo<>(estateList);
    }

}
